/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.iside.android.showcase;

import org.springframework.android.showcase.location.CustomerLayout;
import org.springframework.android.showcase.location.TaxiLayout;
import org.springframework.android.showcase.usercenter.UserCenterLayout;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

/**
 * @author Roy Clarkson
 */
public abstract class AbstractTabActivity extends TabActivity implements OnTabChangeListener {

    private TabHost myTabhost;

    private TabWidget mTabWidget;

    // ***************************************
    // Activity methods
    // ***************************************
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhost_activity_layout);

        myTabhost = this.getTabHost();
        /* 去除标签下方的白线 */
        myTabhost.setPadding(myTabhost.getPaddingLeft(), myTabhost.getPaddingTop(), myTabhost.getPaddingRight(),
                myTabhost.getPaddingBottom() - 5);

        String[] items = getTabItems();

        Intent layout1intent = new Intent();
        layout1intent.setClass(this, CustomerLayout.class);
        TabHost.TabSpec layout1spec = myTabhost.newTabSpec("customer");
        layout1spec.setIndicator(items[0], getResources().getDrawable(R.drawable.customer));
        layout1spec.setContent(layout1intent);
        myTabhost.addTab(layout1spec);

        Intent layout2intent = new Intent();
        layout2intent.setClass(this, TaxiLayout.class);
        TabHost.TabSpec layout2spec = myTabhost.newTabSpec("taxi");
        layout2spec.setIndicator(items[1], getResources().getDrawable(R.drawable.taxi));
        layout2spec.setContent(layout2intent);
        myTabhost.addTab(layout2spec);

        Intent layout3intent = new Intent();
        layout3intent.setClass(this, UserCenterLayout.class);
        TabHost.TabSpec layout3spec = myTabhost.newTabSpec("usercenter");
        layout3spec.setIndicator(items[2], getResources().getDrawable(R.drawable.user_info));
        layout3spec.setContent(layout3intent);
        myTabhost.addTab(layout3spec);

        /* 对Tab标签的定制 */
        mTabWidget = myTabhost.getTabWidget();
        for (int i = 0; i < mTabWidget.getChildCount(); i++) {
            /* 得到每个标签的视图 */
            View view = mTabWidget.getChildAt(i);
//            view.setBackgroundColor(Color.rgb(184, 228, 138));
            /* 设置每个标签的背景 */
            // if (myTabhost.getCurrentTab() == i)
            // {
            // view.setBackgroundDrawable(getResources().getDrawable(
            // R.drawable.number_bg_pressed));
            // }
            // else
            // {
            // view.setBackgroundDrawable(getResources().getDrawable(
            // R.drawable.number_bg));
            // }
            /* 设置Tab间分割竖线的颜色 */
//            mTabWidget.setBackgroundColor(Color.WHITE);
            /* 设置Tab间分割竖线的背景图片 */
            // tabWidget.setBackgroundResource(R.drawable.icon);
            /* 设置tab的高度 */
            mTabWidget.getChildAt(i).getLayoutParams().height = 100;
            TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title);
            /* 设置tab内字体的颜色 */
            tv.setTextColor(Color.rgb(240, 163, 14));
        }
        // LayoutInflater.from(this).inflate(R.layout.tabhost_activity_layout, myTabhost.getTabContentView(), true);
        // myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));
        //
        // String[] items = getTabItems();
        // myTabhost.addTab(myTabhost.newTabSpec("one")
        // .setIndicator(items[0], getResources().getDrawable(R.drawable.customer))
        // .setContent(R.id.widget_layout_Blue));
        //
        // myTabhost.addTab(myTabhost.newTabSpec("two")
        // .setIndicator(items[1], getResources().getDrawable(R.drawable.taxi))
        // .setContent(R.id.widget_layout_green));
        //
        // myTabhost.addTab(myTabhost.newTabSpec("three")
        // .setIndicator(items[2], getResources().getDrawable(R.drawable.user_info))
        // .setContent(R.id.widget_layout_red));
        //
        // myTabhost.setOnTabChangedListener(this);

    }

    // ***************************************
    // Abstract methods
    // ***************************************
    protected abstract String getDescription();

    protected abstract String[] getMenuItems();

    protected abstract String[] getTabItems();

    protected abstract OnItemClickListener getMenuOnItemClickListener();
}
